package com.yzx.controller.system;

import com.yzx.aspectj.annotation.Log;
import com.yzx.aspectj.enums.BusinessType;
import com.yzx.service.SysRoleService;
import com.yzx.utils.ShiroSecurityUtils;
import com.yzx.vo.AjaxResult;
import com.yzx.vo.DataGridView;
import dto.SysRoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/role")
public class RoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("listRoleForPage")
    public AjaxResult listRoleForPage(SysRoleDto sysRoleDto){
        DataGridView dataGridView = sysRoleService.listRolePage(sysRoleDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }
    @PostMapping("addRole")
    @Log(title = "添加角色",businessType = BusinessType.INSERT)
    public AjaxResult addRole(@Validated SysRoleDto sysRoleDto){
        sysRoleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(sysRoleService.addRole(sysRoleDto));
    }
    @PutMapping("updateRole")
    @Log(title = "修改角色",businessType = BusinessType.UPDATE)
    public AjaxResult updateRole(@Validated SysRoleDto sysRoleDto){
        sysRoleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(sysRoleService.updateRole(sysRoleDto));
    }
    @GetMapping("getRoleById/{roleId}")
    public AjaxResult getRoleById(@PathVariable Long roleId){
        return AjaxResult.success("查询成功",sysRoleService.getOne(roleId));
    }
    @DeleteMapping("deleteRoleByIds/{roleIds}")
    @Log(title = "删除角色",businessType = BusinessType.DELETE)
    public AjaxResult deleteRoleByIds(@PathVariable Long[] roleIds){
        return AjaxResult.toAjax(sysRoleService.deleteRoleByIds(roleIds));
    }

    @GetMapping("queryMenuIdByRoleId/{roleId}")
    public AjaxResult queryMenuIdByRoleId(@PathVariable Long roleId){
        List<Long> longs = this.sysRoleService.queryMenuIdsByRoleId(roleId);
        return AjaxResult.success("查询成功",longs);
    }
    @PostMapping("saveRoleMenu/{roleId}/{menuIds}")
    @Log(title = "角色授权",businessType = BusinessType.INSERT)
    public AjaxResult saveRoleMenu(@PathVariable Long roleId,@PathVariable Long[] menuIds){
//        if (menuIds.length==1&&menuIds[0].equals(-1)){
//            menuIds=new Long[]{};
//        }
        sysRoleService.saveRoleMenu(roleId,menuIds);
        return AjaxResult.success("授权成功");
    }
}
